package com.mezz.bones.im.gateway.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.mezz.bones.im.common.domain.msg.MessageSend2UserConnect;
import com.mezz.bones.im.common.domain.msg.MessageSend2UserId;
import com.mezz.bones.im.common.domain.user.ImUserConnectInfo;
import com.mezz.bones.im.common.result.ImResult;
import com.mezz.bones.im.common.result.ImResultCode;
import com.mezz.bones.im.gateway.service.IMessageSenderService;
import com.mezz.bones.im.gateway.service.IUserChannelService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhuang
 * @Date: 2025/1/8 13:46
 * @Description: TODO
 * @Version: 1.0
 **/

@Service
@Slf4j
public class MessageSenderServiceImpl implements IMessageSenderService {

    @Resource
    private IUserChannelService userChannelService;

    @Override
    public void sendPrivateMessage2UserId(MessageSend2UserId messageSend2UserId) {

        String userId = messageSend2UserId.getUserId();
        String traceId = messageSend2UserId.getTraceId();
        String actionCode = messageSend2UserId.getActionCode();
        Object data = messageSend2UserId.getData();
        ImResultCode resultCode = messageSend2UserId.getResultCode();

        List<ImUserConnectInfo> allUserChannel = userChannelService.allUserChannel(userId);
        if(CollUtil.isEmpty(allUserChannel)){
           log.warn("user={} channel is empty",userId);
        }

        for (ImUserConnectInfo imUserConnectInfo : allUserChannel) {
            sendPrivateMessage2UserConnect(MessageSend2UserConnect.builder()
                    .userConnectInfo(imUserConnectInfo)
                    .traceId(traceId)
                    .actionCode(actionCode)
                    .data(data)
                    .resultCode(resultCode)
                    .build());
        }
    }

    @Override
    public void sendPrivateMessage2UserConnect(MessageSend2UserConnect messageSend2UserConnect) {

        String traceId = messageSend2UserConnect.getTraceId();
        String actionCode = messageSend2UserConnect.getActionCode();
        Object data = messageSend2UserConnect.getData();
        ImResultCode resultCode = messageSend2UserConnect.getResultCode();
        ImUserConnectInfo userConnectInfo = messageSend2UserConnect.getUserConnectInfo();

        try {
            Channel channel = userChannelService.getChannelByUserInfoFromMap(userConnectInfo);
            if(channel == null){
                log.error("user[{}] connect not found",userConnectInfo);
                //连接已断开  但是 redis上状态可能还没有清楚 可能是系统重启
                userChannelService.forceOffLine(userConnectInfo);
                return;
            }
            channel.writeAndFlush(ImResult.data(resultCode,traceId,actionCode,data));
        } catch (Exception e){
          log.error("send private message to user[{}] connect error",userConnectInfo,e);
        }
    }
}
